﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data;
using System.Data.SqlClient;

namespace DAO
{
    public class PhieuThuTienDAO
    {
        #region 1.Retrieving
        public static List<PhieuThuTienDTO> SellectAllPhieuThuTien()
        {
            List<PhieuThuTienDTO> listPhieuThu = new List<PhieuThuTienDTO>();

            DataTable dt = SqlDataConnectHelper.ExecuteQuery("usp_SelectPhieuThuTiensAll");
            foreach (DataRow dr in dt.Rows)
            {
                PhieuThuTienDTO phieuThu = new PhieuThuTienDTO();

                phieuThu.MaDaiLy = dr["MaDaiLy"].ToString();
                phieuThu.MaPhieuThuTien = dr["MaPhieuThu"].ToString();
                phieuThu.NgayThuTien = DateTime.Parse(dr["NgayThuTien"].ToString());
                phieuThu.SoTienThu = float.Parse(dr["SoTienThu"].ToString());

                listPhieuThu.Add(phieuThu);
            }

            return listPhieuThu;
        }
        public static List<PhieuThuTienDTO> SellectAllPhieuThuTienByID(string id)
        {
            List<PhieuThuTienDTO> listPhieuThu = new List<PhieuThuTienDTO>();

            List<SqlParameter> listSqlParam = new List<SqlParameter>();
            listSqlParam.Add(new SqlParameter("@MaPhieuThu", id));
            DataTable dt = SqlDataConnectHelper.ExecuteQuery("usp_SelectPhieuThuTiensByAndMaPhieuThu", listSqlParam);
            if (dt.Rows.Count == 1)
            {
                PhieuThuTienDTO phieuThu = new PhieuThuTienDTO();
                phieuThu.MaDaiLy = dt.Rows[0]["@MaDaiLy"].ToString();
                phieuThu.MaPhieuThuTien = dt.Rows[0]["@MaPhieuThu"].ToString();
                phieuThu.NgayThuTien = DateTime.Parse(dt.Rows[0]["@NgayThuTien"].ToString());
                phieuThu.SoTienThu = float.Parse(dt.Rows[0]["@SoTienThu"].ToString());

                listPhieuThu.Add(phieuThu);
            }

            return listPhieuThu;
        }
        public static bool CheckIDPhieuThuTien(string id)
        {
            List<PhieuThuTienDTO> listPhieuThu = new List<PhieuThuTienDTO>();

            List<SqlParameter> listSqlParam = new List<SqlParameter>();
            listSqlParam.Add(new SqlParameter("@MaPhieuThu", id));
            DataTable dt = SqlDataConnectHelper.ExecuteQuery("usp_SelectPhieuThuTiensByAndMaPhieuThu", listSqlParam);
            foreach (DataRow dr in dt.Rows)
            {
                PhieuThuTienDTO phieuThu = new PhieuThuTienDTO();

                phieuThu.MaDaiLy = dt.Rows[0]["MaDaiLy"].ToString();
                phieuThu.MaPhieuThuTien = dt.Rows[0]["MaPhieuThu"].ToString();
                phieuThu.NgayThuTien = DateTime.Parse(dt.Rows[0]["NgayThuTien"].ToString());
                phieuThu.SoTienThu = float.Parse(dt.Rows[0]["SoTienThu"].ToString());

                listPhieuThu.Add(phieuThu);
            }

            if (listPhieuThu.Count == 1)
            {
                return true;
            }
            else
                return false;
        }
        #endregion
        #region 2.Inserting
        public static bool InsertPhieuThu(PhieuThuTienDTO phieuThu)
        {

            List<SqlParameter> listSqlParam = new List<SqlParameter>();
            listSqlParam.Add(new SqlParameter("@MaPhieuThu", phieuThu.MaPhieuThuTien));
            listSqlParam.Add(new SqlParameter("@MaDaiLy", phieuThu.MaDaiLy));
            listSqlParam.Add(new SqlParameter("@NgayThuTien", phieuThu.NgayThuTien));
            listSqlParam.Add(new SqlParameter("@SoTienThu", phieuThu.SoTienThu));

            int n = SqlDataConnectHelper.ExecuteNoneQuery("usp_InsertPhieuThuTien", listSqlParam);
            if (n == -1)
            {
                return true;
            }
            else
                return false;


        }
        #endregion
    }
}
